<template>
  <view class="tips">
		<view v-for="(item, index) in tips" :key="index" @click="handleClick(item)" >
			<tips-card :title="item.title" :photo="item.photo" :time="item.createTime"></tips-card>
		</view>
    <!-- <view v-if="tips.length !== 0">
			<uni-pagination
					:total="total"
					:current="pageQuery.pageNum"
					:pageSize="pageQuery.pageSize"
					@change="handlePageChange"
			>
			</uni-pagination>
		</view> -->
	</view>
</template>

<script>
import TipsCard from '@/components/TipsCard'
import {getTips} from '../../../api/tips'
import {uniPagination} from '@dcloudio/uni-ui'
export default {
	name: 'tips',
	components: {
		TipsCard, uniPagination
	},
	methods: {
		handleClick(item) {
			console.log(item)
			uni.navigateTo({
        url: '/pages/home/home-tips/tipsDetail?id=' + item.id
      })
		},
		getData() {
			const data = {
				pageNum: this.pageQuery.pageNum,
				pageSize: this.pageQuery.pageSize
			}
			getTips(data).then(res => {
				console.log(res)
				if (res.code === 200) {
					this.tips = res.data.list
					this.total = res.data.total
				} else {
					uni.showToast({
						icon:'none',
						title: res.message
					});
				}
			})
		},
		getMoreData() {
			if (this.loadingType !== 0) {//loadingType!=0;直接返回
				return false;
			}
			this.loadingType = 1
			this.pageQuery.pageNum++
			uni.showNavigationBarLoading()
			const data = {
				pageNum: this.pageQuery.pageNum,
				pageSize: this.pageQuery.pageSize
			}
			getTips(data).then(res => {
				console.log(res)
				uni.hideNavigationBarLoading()
				if (res.code === 200) {
					this.tips = res.data.list
					this.total = res.data.total
					if (res.data.list.length === 0) {
						this.loadingType = 2
						uni.hideNavigationBarLoading()//关闭加载动画
						return
					}
					var newData = res.data.list;
					this.tips = this.tips.concat(newData)//将数据拼接在一起
					this.loadingType = 0//将loadingType归0重置
						uni.hideNavigationBarLoading()//关闭加载动画	
				} else {
					uni.showToast({
						icon:'none',
						title: res.message
					})
				}
			})
		},
		handlePageChange(val) {
			this.pageQuery.pageNum = val.current
			this.getTipList()
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		console.log('下拉刷新')
		this.pageQuery.pageNum = 1
		this.loadingType = 0
		this.getData()
		uni.stopPullDownRefresh()
	},
	//触底加载更多
	onReachBottom(e) {
		console.log('触底加载更多')
		this.getMoreData();
	},
	data() {
		return {
			pageQuery: {
				pageSize: 11,
				pageNum: 1
			},
			tips: [],
			total: 0,
			loadingText: '加载中...',
			loadingType: 0,
			contentText: {
				contentdown:'上拉显示更多',
				contentrefresh: '正在加载...',
				contentnomore: '没有更多数据了'
			},
		}
	},
	mounted() {
		// this.getTipList()
		this.getData()
	}
}
</script>

<style>

</style>